/**
 * Created by Qingger Corp.
 * User: jsspf
 * Date: 2017/12/14
 * Time: 17:14
 */

/**
 * TypeScript 在 2.4 版本中引入的一个新特性 ThisType。
 * ThisType 本身是一个不包含内容的 interface，其作用是人为地给定某些条件下 this 的类型
 */

type ObjectDescriptor<D, M> = {
    data?: D;
    methods?: M & ThisType<D & M>;  // Type of 'this' in methods is D & M
}


function makeObject<D,M>(desc : ObjectDescriptor<D, M>) : D & M {
   let data : object = desc.data || {};
   let methods : object = desc.methods || {};
   return {...data,...methods} as D & M;
}

let ttObj = makeObject({
   data : {x:0,y:0},
   methods : {
      moveBy(dx:number,dy:number) {
         this.x = x;
         this.y = y;
      }
   }
});

ttObj.x = 10;
ttObj.y = 20;